package com.zy.controller;


import com.zy.bean.TAdmin;
import com.zy.bean.TAdminExample;
import com.zy.bean.TMember;
import com.zy.bean.TMenu;
import com.zy.service.TAdminService;
import com.zy.service.TMemberService;
import com.zy.service.TMenuService;
import com.zy.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.mail.Session;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class DispatcherController {
    @Autowired
    TAdminService tAdminService;
    @Autowired
    TMenuService tMenuService;
    @Autowired
    TMemberService tMemberService;
    Logger log = LoggerFactory.getLogger(DispatcherController.class);

    @RequestMapping("/index")
    public String index() {
        log.debug("跳转到系统主页面");
        return "index";
    }

    @RequestMapping("/toLogin")
    public String login() {
        log.debug("==debug==跳转到登录页面");
        log.debug("debug消息id={},name={}",1,"zsl");
        log.info("==info=跳转到登录页面");
        log.warn("=warn==跳转到登录页面");
        log.error("==error=跳转到登录页面");
        return "login";
    }

    @RequestMapping("/reg")
    public String reg() {
        log.debug("===跳转到注册页面");
        log.debug("debug消息id={},name={}",1,"zsl");
        log.info("===跳转到注册页面");
        log.warn("===跳转到注册页面");
        log.error("===跳转到注册页面");
        return "reg";
    }

    @RequestMapping("/main")
    public String main(HttpSession session) {
        log.debug("==跳转到首页页面");
        if (session == null) {
            return "redirect:/toLogin";
        }
//存放到session域中防止刷新后---一直查库
        List<TMenu> menuLists = (List<TMenu>) session.getAttribute("menuList");
        if (menuLists == null) {
            menuLists = tMenuService.getList();
            session.setAttribute("menuList", menuLists);
        }
        return "main";

    }
     /**
     *@Author: zy 690748807@qq.com
     * @Param:
     * @return:
     * @Date: 2024/3/3
     * @Description: 注销页面---跳转到登陆页
     */
     @RequestMapping("/logout")
     public String logout() {
         log.debug("跳转到登陆页");
         return "login";
     }
 /**
 *@Author: zy 690748807@qq.com
 * @Param:
 * @return:
 * @Date: 2024/3/2
 * @Description: 自己写登陆不用里使用安全框架验证登陆111
 */
//    @RequestMapping("/doLogin")
//    public String doLogin(String loginacct, String userpaswd, HttpSession session, Model model) {
//        log.debug("去登录页面======后台控制页面");
//        log.info("loginacct={}", loginacct);
//        log.debug("userpswd={}", userpaswd);
//        Map<String, Object> maptadmin = new HashMap<>();
//        maptadmin.put("loginacct", loginacct);
//        maptadmin.put("userpaswd", userpaswd);
//        try {
//            TAdmin tAdminByLogin = tAdminService.getTAdminByLogin(maptadmin);
//            session.setAttribute(Const.LOGIN_ADMIN, tAdminByLogin);
//            log.debug("登录成功...");
//            //return "main"; //避免表单重复提交，采用重定向
//            return "redirect:/main";
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.debug("登录失败={}...", e.getMessage());
//            model.addAttribute("message", e.getMessage());
//            return "login";
//        }
//
//
//    }

    /*
    注销页面
     */
//    @RequestMapping("/logout")
//    public String logout(HttpSession session) {
//        if (session != null) {
//            //销毁session
//            session.removeAttribute(Const.LOGIN_ADMIN);
//            session.invalidate();
//        }
//        log.debug("跳转到系统主页面");
//        return "redirect:/index";
//
//    }

    /*
    注册成会员信息
     */
    @RequestMapping("/domember")
    public String member(TMember tMember) {
        /*
        注册-----会员表和管理员表添加数据
         */
        int insert = tMemberService.insert(tMember);
        TAdmin tAdmin = new TAdmin();
        String loginacct = tMember.getLoginacct();
        String email = tMember.getEmail();
        tAdmin.setLoginacct(loginacct);
        tAdmin.setEmail(email);
        tAdmin.setUsername(loginacct);
        tAdminService.saveAdmin(tAdmin);
        if (insert > 0) {
            return "redirect:/login";
        }
        return "reg";
    }
}
